package com.events.testservice.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

/**
 * Sales order entity object.
 * 
 * @author Ashish Sonawane
 * 
 */
@Entity
public class SalesOrderEntity {

	@Column(name = "product")
	@OneToMany(mappedBy = "salesOrderEntity", fetch = FetchType.LAZY)
	List<ProductEntity> product;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long salesOrderId;

	@Column(name = "ordername")
	private String ordername;

	public List<ProductEntity> getProduct() {
		return product;
	}

	public void setProduct(List<ProductEntity> product) {
		this.product = product;
	}

	public Long getSalesOrderId() {
		return salesOrderId;
	}

	public void setSalesOrderId(Long salesOrderId) {
		this.salesOrderId = salesOrderId;
	}

	public String getOrdername() {
		return ordername;
	}

	public void setOrdername(String ordername) {
		this.ordername = ordername;
	}

	/**
	 * Builder object for sales order.
	 * 
	 * @author Ashish Sonawane
	 * 
	 */
	public static class Builder {

		private Long salesOrderId;
		private String ordername;
		List<ProductEntity> productList;

		public Builder id(Long salesOrderId) {
			this.salesOrderId = salesOrderId;
			return this;
		}

		public Builder name(String ordername) {
			this.ordername = ordername;
			return this;
		}

		public Builder product(List<ProductEntity> productList) {
			this.productList = productList;
			return this;
		}

		public SalesOrderEntity build() {
			return new SalesOrderEntity(this);
		}

	}

	private SalesOrderEntity(Builder builder) {
		this.salesOrderId = builder.salesOrderId;
		this.ordername = builder.ordername;
		this.product = builder.productList;
	}

	public SalesOrderEntity() {
		super();
	}

	@Transient
	public double getSalesOrderTotal(List<ProductEntity> productList) {
		double total = 0.0F;
		for (ProductEntity entity : productList) {
			total += entity.getPrice();
		}
		return total;
	}

}
